<template>
  <el-dialog
    title="配置权限"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="">
      <el-checkbox v-for="item in permissionList" v-model="item.permissionChecked" :key="item.permissionId" :label="item.permissionName+item.permissionExpression" :value="item.permissionId" border @change="changePermission"></el-checkbox>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        roleId: '',
        permissionList: [],
        dataRule: {
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.roleId = id
        this.$nextTick(() => {
          this.$http({
            url: this.$http.adornUrl(`/partner/partnerpermission/queryList?roleId=`+this.roleId),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.permissionList = data.permissionList
            }
          })
        })
      },
      changePermission: function (data) {
      },
      // 表单提交
      dataFormSubmit () {
        var permissionIdList = new Array();
        for(var i = 0; i < this.permissionList.length; i++){
          if(this.permissionList[i].permissionChecked){
            permissionIdList.push(this.permissionList[i].permissionId)
          }
        }
        this.$http({
          url: this.$http.adornUrl(`/partner/partnerrolepermission/configPermission?roleId=`+this.roleId+`&permissionIdList=`+permissionIdList.join(",")),
          method: 'post',
          data: {
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
